package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.model.User;
import com.prince.page.Page;
import com.prince.util.PoiExcelExportUtil;
import com.prince.web.base.BaseController;
import com.prince.web.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 用户
 * @author Wangpeixi 
 *
 */
@Controller
public class UserController extends BaseController{

	@Resource
	private UserService userService;
	
	/**
	 * 分页查询
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findUserByPage", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findUserByPage(@RequestParam Map<String,Object> params,HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		params.put("user", user);
		Page page = this.getRequestPageRollInfo(params);
		Map<String,Object> map = userService.findUserByPage(params,page);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 添加
	 * @param 
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "addUser", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> addUser(User user,HttpServletRequest request) {
		User u = (User) request.getSession().getAttribute("user");
		Map<String,Object> map = userService.addUser(user,u);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 修改
	 * @param 
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "updateUser", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> updateUser(User user) {
		Map<String,Object> map = userService.updateUser(user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 删除
	 * @param 
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "delUser", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> delUser(User user) {
		Map<String,Object> map = userService.delUser(user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	
	/**
	 * 根据id查询用户
	 * @param 
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findUserByCondition", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findUserByCondition(User user) {
		Map<String,Object> map = userService.getUserById(user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 根据条件查询用户
	 * @param user
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findAllUserByCondition", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findAllUserByCondition(User user) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<User> users= userService.findUserByCondition(user);
		map.put("data", users);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	/**
	 * 修改密码
	 * @param user
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "updatePwd", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> updatePwd(User user,HttpServletRequest request) {
		User u = (User) request.getSession().getAttribute("user");
		Map<String,Object> map = userService.updatePwd(user,u);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 查询昵称
	 * @param request
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findParentUserName", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findParentUserName(HttpServletRequest request) {
		User u = (User) request.getSession().getAttribute("user");
		Map<String,Object> map = userService.findParentUserName(u);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	
	/**
	 * 导出
	 * @param params
	 * @param response
	 * @param request
	 */
	@AppLog
	@RequestMapping(value = "exportUser", method = RequestMethod.GET)
	public void exportText(@RequestParam Map<String, Object> params,HttpServletResponse response,HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		params.put("user", user);
       Map<String,Object> map = userService.findUserByPage(params,null);
       Object object = map.get("rows");
       if(null == object) {
    	  return;
       }
	    @SuppressWarnings("unchecked")
		List<User> list = (List<User>)object;
        //导出
	    Date date = new Date();
		PoiExcelExportUtil poiExcelExportUtil = new PoiExcelExportUtil();
	    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
	    String strDate = simpleDateFormat.format(date);
		String[] titleColumns = {  "loginName","belongType","linkman", "created"};
		String[] titleNames = {"昵称","用户类型", "登录名", "注册时间"};
		//生成下载流
		//生成下载流
		poiExcelExportUtil.writeExcel(list, titleColumns, titleNames, response, "用户资料-" + strDate);
			
	}
}
